<template>
  <div class="order-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>订单列表</span>
          <el-button type="primary" @click="goToCreateOrder">
            <el-icon><Plus /></el-icon>
            新增订单
          </el-button>
        </div>
      </template>
      
      <el-table :data="orders" style="width: 100%">
        <el-table-column prop="id" label="订单号" width="120" />
        <el-table-column prop="customerName" label="客户姓名" width="120" />
        <el-table-column prop="productName" label="产品名称" width="150" />
        <el-table-column prop="amount" label="金额" width="100">
          <template #default="scope">
            ¥{{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="viewOrder(scope.row.id)">
              查看
            </el-button>
            <el-button size="small" type="warning" @click="editOrder(scope.row.id)">
              编辑
            </el-button>
            <el-button size="small" type="danger" @click="deleteOrder(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const router = useRouter()

const orders = ref([
  {
    id: 'ORD001',
    customerName: '张三',
    productName: 'iPhone 15',
    amount: 7999,
    status: 'pending',
    createdAt: '2024-01-15 10:30:00'
  },
  {
    id: 'ORD002',
    customerName: '李四',
    productName: 'MacBook Pro',
    amount: 15999,
    status: 'completed',
    createdAt: '2024-01-16 14:20:00'
  },
  {
    id: 'ORD003',
    customerName: '王五',
    productName: 'iPad Air',
    amount: 4599,
    status: 'cancelled',
    createdAt: '2024-01-17 09:15:00'
  }
])

const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    pending: 'warning',
    completed: 'success',
    cancelled: 'danger'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    pending: '待处理',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || '未知'
}

const goToCreateOrder = () => {
  router.push('/orders/create')
}

const viewOrder = (id: string) => {
  router.push(`/orders/${id}`)
}

const editOrder = (id: string) => {
  ElMessage.info(`编辑订单 ${id}`)
}

const deleteOrder = async (id: string) => {
  try {
    await ElMessageBox.confirm('确定要删除这个订单吗？', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    orders.value = orders.value.filter(order => order.id !== id)
    ElMessage.success('删除成功')
  } catch {
    ElMessage.info('已取消删除')
  }
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>